package br.com.nrti.still;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import br.com.nrti.still.eventos.Toque;

import com.actionbarsherlock.app.SherlockFragment;

public class TabEventos_Geral extends SherlockFragment {

	/**
	 * Constantes
	 */
	public final int BOTAO_INICIO = 1;
	public final int BOTAO_FINAL = 2;
	public final int BOTAO_DATA = 3;

	/**
	 * Controles da tela
	 */
	EditText etNomeEvento;
	Button btHoraInicio;
	Button btHoraFinal;
	CheckBox cbRepetir;
	CheckBox cbDom;
	CheckBox cbSeg;
	CheckBox cbTer;
	CheckBox cbQua;
	CheckBox cbQui;
	CheckBox cbSex;
	CheckBox cbSab;
	Button btDataEvento;
	LinearLayout painelDiasSemana;
	LinearLayout painelDataEvento;

	/**
	 * Captura de data e hora
	 */
	int inicio_hor;
	int inicio_min;
	int final_hor;
	int final_min;
	int data_dia;
	int data_mes;
	int data_ano;

	private TimePickerDialog.OnTimeSetListener mInicio = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			inicio_hor = hourOfDay;
			inicio_min = minute;
			updateButton(BOTAO_INICIO);
		}
	};

	private TimePickerDialog.OnTimeSetListener mFinal = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			final_hor = hourOfDay;
			final_min = minute;
			updateButton(BOTAO_FINAL);
		}
	};

	private DatePickerDialog.OnDateSetListener mDataEvento = new OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			data_ano = year;
			data_mes = monthOfYear;
			data_dia = dayOfMonth;
			updateButton(BOTAO_DATA);
		}
	};

	public void updateButton(int tipoBotao) {
		switch (tipoBotao) {
		case BOTAO_INICIO:
			btHoraInicio.setText(Util.formataHora(inicio_hor, inicio_min));
			break;
		case BOTAO_FINAL:
			btHoraFinal.setText(Util.formataHora(final_hor, final_min));
			break;
		case BOTAO_DATA:
			btDataEvento
					.setText(Util.formataData(data_dia, data_mes, data_ano));
		default:
			break;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		final Calendar cal = Calendar.getInstance();
		data_ano = cal.get(Calendar.YEAR);
		data_mes = cal.get(Calendar.MONTH);
		data_dia = cal.get(Calendar.DAY_OF_MONTH);

		inicio_hor = cal.get(Calendar.HOUR_OF_DAY);
		inicio_min = cal.get(Calendar.MINUTE);

		final_hor = cal.get(Calendar.HOUR_OF_DAY);
		final_min = cal.get(Calendar.MINUTE);
		
		Toque.inicializar(TabEventos_Geral.this.getSherlockActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.f_evento_geral, container, false);
		etNomeEvento = (EditText) view.findViewById(R.id.et_NomeEvento);
		btHoraInicio = (Button) view.findViewById(R.id.btInicio);
		btHoraFinal = (Button) view.findViewById(R.id.btFinal);
		cbRepetir = (CheckBox) view.findViewById(R.id.cbRepetir);
		cbDom = (CheckBox) view.findViewById(R.id.cbdom);
		cbSeg = (CheckBox) view.findViewById(R.id.cbseg);
		cbTer = (CheckBox) view.findViewById(R.id.cbter);
		cbQua = (CheckBox) view.findViewById(R.id.cbqua);
		cbQui = (CheckBox) view.findViewById(R.id.cbqui);
		cbSex = (CheckBox) view.findViewById(R.id.cbsex);
		cbSab = (CheckBox) view.findViewById(R.id.cbsab);
		btDataEvento = (Button) view.findViewById(R.id.btDataEvento);
		painelDataEvento = (LinearLayout) view.findViewById(R.id.pnDataEvento);
		painelDiasSemana = (LinearLayout) view.findViewById(R.id.pnDiasSemana);

		painelDataEvento.setVisibility(View.GONE);

		cbRepetir.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				painelDataEvento.setVisibility(arg1 ? View.GONE : View.VISIBLE);
				painelDiasSemana.setVisibility(arg1 ? View.VISIBLE : View.GONE);
			}
		});

		btHoraInicio.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog dialogo = new TimePickerDialog(TabEventos_Geral.this
						.getSherlockActivity(), mInicio, inicio_hor,
						inicio_min, false);
				dialogo.show();
			}
		});

		btHoraFinal.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog dialogo = new TimePickerDialog(TabEventos_Geral.this
						.getSherlockActivity(), mFinal, final_hor, final_min,
						false);
				dialogo.show();
			}
		});

		btDataEvento.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog dialogo = new DatePickerDialog(TabEventos_Geral.this
						.getSherlockActivity(), mDataEvento, data_ano,
						data_mes, data_dia);
				dialogo.show();
			}
		});

		updateButton(BOTAO_INICIO);
		updateButton(BOTAO_FINAL);
		updateButton(BOTAO_DATA);

		return view;
	}
}
